package heap

import "errors"

type tHeapIterator struct {
	heap *tArrayHeap
	pos int
	version int64
}

func newHeapIterator(heap *tArrayHeap) IIterator {
	return &tHeapIterator{
		heap: heap,
		pos: 0,
		version: heap.version,
	}
}

func (me *tHeapIterator) More() bool {
	if me.version != me.heap.version {
		return false
	}
	return me.pos < me.heap.size
}

func (me *tHeapIterator) Next() (error, interface{}) {
	if me.version != me.heap.version {
		return gConcurrentModificationError, nil
	}

	if me.pos >= me.heap.size {
		return gNoMoreElementsError, nil
	}

	v := me.heap.items[me.pos]
	me.pos++
	return nil, v
}

var gConcurrentModificationError = errors.New("concurrent modification error")